<?php

/**
 *  测试
*/

namespace app\api\controller;


use app\api\service\MailService;
use app\common\service\Inventory;
use app\common\shopify\FulfillmentService;
use app\common\shopify\OrderService;
use app\common\shopify\ProductService;
use app\facade\Mail;
use app\Request;
use coinpal\Payment;
use coinpal\PaymentException;

class Test extends BaseController
{


    public function test(Request $request)
    {

        $comdition = [
            [
                'product_id' => 210,
                'arrival_date' => null,
                'is_futures' => 0,
                'wid' => 9,
                'quantity' => 6,
            ],[
                'product_id' => 211,
                'arrival_date' => null,
                'is_futures' => 0,
                'wid' => 9,
                'quantity' => 10,
            ]
        ];
       $re =  (new Inventory())->unlockInventory($comdition);
        dd($re);
        $productId = [
            "order_id" => "400083",
  "logistics_company" => 6,
  "wid" => 27,
  "list" =>  [
       [
        "id" => 9882,
      "wid" => 27,
      "fl_warehouse_id" => 26,
      "code" => "GoSh-E-AE1M",
      "name" => "【NEW Release】Goldshell E-AE1M 230MH 2000W ALEO Miner",
      "hashrate" => "230.003",
      "expiration_date" => null,
      "is_futures" => 0,
      "arrival_date" => null,
      "real_arrival_date" => null,
      "original_price" => "629.95",
      "actual_price" => "629.95",
      "free_shipping" => 0,
      "apply_rules" => 1,
      "quantity" => 5,
      "brand" => 3,
      "product_id" => 236,
      "pending_quantity" => 1,
      "actual_quoted_currency" => 214,
      "actual_selling_price" => "0.00",
      "actual_quoted_currency_name" => "美元",
      "ship_quantity" => 1,
    ]
  ],
  "logistics_no" => "11111212",
  "logistics_order" => 1,
  "logistics_voucher" => [],
  "notes" => "",
  "cost" => "",
  "logistics_fee" => 0.0,
  "shop_fl_warehouse" => 0,
  "shippinAddress" =>  [
        "first_name" => "sad",
    "last_name" => "asd",
    "zip_code" => "",
    "country_id" => 2,
    "city_id" => 316,
    "city_name" => null,
    "state_id" => null,
    "address" => "asdasd",
    "address2" => "",
    "company" => "asd",
    "phone" => "+86 13899909090",
    "email" => "xuzhong@jinglemining.com",
  ],
  "ship_date" => "2025-07-17",
  "operator_id" => 10
]; // 本地商品ID
        $service = new FulfillmentService();

        $shopifyId = $service->createOrUpdateFulfillment($productId);
        dd($shopifyId);


        $config = [
            'debug'=>true,
            'version'=>'2',
            'merchantNo'=>'100005025',
            'apiKey'=>'bb245b3f08975114d1b1bd3e58dd45e072ed2798ab0de492bb3fa357a770b86a',
        ];

        $payment = new Payment();

        try{
            $data['requestId'] = $this->getRequestId();//请求流水号，每次请求需唯一
            $data['merchantName'] = 'Cionpal';//收银台页面商户名
            $data['orderNo'] = $this->orderNo();
            $data['orderCurrencyType'] = 'fiat';//币种类型 crypto 或者 fiat
            $data['orderCurrency'] = 'USD';//订单币种
            $data['orderAmount'] = '10.5';//订单金额
            $data['orderDescription'] = 'Iphone 14';//订单描述 将用于特定收银台，页面展示
            $data['payerIP'] = $request->ip();//付款人设备IP

            $data['notifyURL'] = 'https:?order='.$data['orderNo']; //商戶異步通知地址
            $data['redirectURL'] = 'https://www.order-test.cn'; // 用戶支付成功/支付過期後，前台頁面回調地址
            $data['cancelURL'] = 'https://www.order-test.cn'; // 用戶點擊支付取消後，前台頁面回調地址。如果未傳值，則默認回調到
            $data['successUrl'] = 'https://www.order-test.cn'; //支付成功跳轉地址 如果為空 則默認使用

            $data['remark'] = 'order'; // 普通订单 order 采购订单  wholesal


            $result =  $payment->setMerchantNo($config['merchantNo'])->setVersion($config['version'])->setApiKey($config['apiKey'])->create($data);


            print_r($result);
            die;

            echo "<pre>";
            print_r($result);
            echo "</pre>";
            //保存gcid 和支付链接 并且发送到前端调用该地址
        }catch ( PaymentException $e){

            $payment->log($e);
            //记录错误信息
            echo $e->getMessage();
        }
    }

    function getRequestId(){
        return 'Q'.date('YmdHis').uniqid();
    }

    function orderNo() {
        return 'D'.sprintf( '%04x%04x%04x%04x',
                mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
                mt_rand( 0, 0xffff ),
                mt_rand( 0, 0x0fff ) | 0x4000
            );
    }



}
